﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class InventoryDAO
    {
        DataServices m_InventoryDAO = new DataServices();

        public DataTable GetAll()
        {
            m_InventoryDAO.Load("GetAllInventory", null);
            return m_InventoryDAO;
        }

        public DataTable GetAllFull()
        {
            m_InventoryDAO.Load("GetAllInventoryFull", null);
            return m_InventoryDAO;
        }

        public DataTable GetById(String inventoryId)
        {
            SqlParameter param = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param.Value = inventoryId;

            m_InventoryDAO.Load("GetInventoryById", param);
            return m_InventoryDAO;
        }

        public DataTable GetByIdFull(String inventoryId)
        {
            SqlParameter param = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param.Value = inventoryId;

            m_InventoryDAO.Load("GetAllInventoryByIdFull", param);
            return m_InventoryDAO;
        }

        public DataTable GetByType(String typeId)
        {
            SqlParameter param = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param.Value = typeId;

            m_InventoryDAO.Load("GetInventoryByType", param);
            return m_InventoryDAO;
        }

        public DataTable GetByTypeFull(String typeId)
        {
            SqlParameter param = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param.Value = typeId;

            m_InventoryDAO.Load("GetInventoryByTypeFull", param);
            return m_InventoryDAO;
        }

        public DataTable GetByDate(DateTime inventoryDate)
        {
            SqlParameter param = new SqlParameter("@InventoryDate", SqlDbType.DateTime);
            param.Value = inventoryDate;

            m_InventoryDAO.Load("GetInventoryByDate", param);
            return m_InventoryDAO;
        }

        public DataTable GetBySupplier(String supplierId)
        {
            SqlParameter param = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param.Value = supplierId;

            m_InventoryDAO.Load("GetInventoryBySupplier", param);
            return m_InventoryDAO;
        }

        public DataTable GetByUser(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;

            m_InventoryDAO.Load("GetInventoryByUser", param);
            return m_InventoryDAO;
        }

        public DataTable GetImportYear(int year)
        {
            SqlParameter param = new SqlParameter("@Year", SqlDbType.Int);
            param.Value = year;

            m_InventoryDAO.Load("GetImportYear", param);
            return m_InventoryDAO;
        }

        public DataTable GetImportMonth(int year, int month)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Year", SqlDbType.Int);
            param[0].Value = year;

            param[1] = new SqlParameter("@Month", SqlDbType.Int);
            param[1].Value = month;

            m_InventoryDAO.Load("GetImportMonth", param);
            return m_InventoryDAO;
        }

        public int Add(InventoryInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param[1].Value = info.TypeId;

            param[2] = new SqlParameter("@InventoryDate", SqlDbType.DateTime);
            param[2].Value = info.InventoryDate;

            param[3] = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param[3].Value = info.SupplierId;

            param[4] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[4].Value = info.UserId;

            param[5] = new SqlParameter("@Total", SqlDbType.Money);
            param[5].Value = info.Total;

            param[6] = new SqlParameter("@Discount", SqlDbType.Money);
            param[6].Value = info.Discount;

            param[7] = new SqlParameter("@Vat", SqlDbType.Money);
            param[7].Value = info.Vat;

            param[8] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[8].Value = info.IsDeleted;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_InventoryDAO.ExecuteUpdate("AddInventory", param);
        }

        public int Update(InventoryInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@TypeId", SqlDbType.VarChar, 6);
            param[1].Value = info.TypeId;

            param[2] = new SqlParameter("@InventoryDate", SqlDbType.DateTime);
            param[2].Value = info.InventoryDate;

            param[3] = new SqlParameter("@SupplierId", SqlDbType.VarChar, 6);
            param[3].Value = info.SupplierId;

            param[4] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[4].Value = info.UserId;

            param[5] = new SqlParameter("@Total", SqlDbType.Money);
            param[5].Value = info.Total;

            param[6] = new SqlParameter("@Discount", SqlDbType.Money);
            param[6].Value = info.Discount;

            param[7] = new SqlParameter("@Vat", SqlDbType.Money);
            param[7].Value = info.Vat;

            param[8] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[8].Value = info.IsDeleted;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_InventoryDAO.ExecuteUpdate("UpdateInventory", param);
        }

        public int UpdateTotal(InventoryInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@Total", SqlDbType.Money);
            param[1].Value = info.Total;

            return m_InventoryDAO.ExecuteUpdate("UpdateInventoryTotal", param);
        }

        public int Delete(String inventoryId)
        {
            SqlParameter param = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param.Value = inventoryId;

            return m_InventoryDAO.ExecuteUpdate("DeleteInventory", param);
        }

        public DataRow AddNewRow()
        {
            return m_InventoryDAO.NewRow();
        }

        public void AddNewInventory(DataRow m_Row)
        {
            m_InventoryDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_InventoryDAO.ExecuteNoneQuery() > 0;
        }
    }
}
